#! usr/bin/env node

const http = require('http'),
      log = console.log;

http.createServer((req,res)=>{
  log(`${req.method} ${req.url} HTTP/${req.httpVersion}`);
  log(req.headers);
  log('');

  log(`Host:${req.headers.host}`);
  log(`User-Agent:${req.headers['user-agent']}`);//数组的方式解析请求头字段，因为变量名中间不能有横线
  log(`Content-Type:${req.headers['content-type']}`);
  log('');

  log(`authorization:${req.headers.authorization}`)
  let auth = req.haders.authorization;
  if(typeof auth !== 'undefined'){
    auth = auth.split(' ');
    if(auth[0] === 'Basic'){
      let buf = new Buffer(auth[1],'base64');
      log(`username & password:${buf.toString('utf8')}`)
    }
  }
  res.end('ok');

}).listen(8080)
